package com.dykj.fireFighting.server.base.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.base.enterprise.dto.EnterpriseDto;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.system.entity.SysDirectory;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.common.starter.redis.service.RedisService;
import com.dykj.fireFighting.server.base.enterprise.mapper.EnterpriseMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 企业信息表
 *
 * @author JacksonTu
 * @date 2018-12-11 13:49:00
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {

    @Autowired
    private ISysDirectoryService sysDirectoryService;
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Autowired
    private RedisService redisService;

    @Override
    public List<Map<String, Object>> selectEnterpriseList(Map<String, Object> par) {
        return this.baseMapper.selectEnterpriseList(par);
    }

    @Override
    public Enterprise saveEnterprise(Enterprise enterprise) {
        boolean flag = this.save(enterprise);
        if (flag) {
            return enterprise;
        }
        return null;
    }

    @Override
    public List<Enterprise> getListByBindingCode(String code) {
        LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Enterprise::getBindingCode, code);
        wrapper.eq(Enterprise::getEnterpriseType, 2);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<Enterprise> queryListByAreaCode(String areaCode, int enterpriseType) {
        Map<String, Object> params = new HashMap<>();
        List<SysDirectory> childSysDirectoryList = new ArrayList<SysDirectory>();
        SysDirectory sysDirectory = sysDirectoryService.queryByCode(areaCode);
        childSysDirectoryList.add(sysDirectory);
        childSysDirectoryList = sysDirectoryService.getChildSysDirectoryList(childSysDirectoryList, areaCode);

        params.put("sysDirectoryList", childSysDirectoryList);
        params.put("enterpriseType", enterpriseType);
        return this.baseMapper.queryListByAreaCode(params);
    }
    @Override
    public List<Enterprise> queryListByAreaCodeOne(String areaCode, int enterpriseType) {
        Map<String, Object> params = new HashMap<>();
        List<SysDirectory> childSysDirectoryList = new ArrayList<SysDirectory>();
        SysDirectory sysDirectory = sysDirectoryService.queryByCode(areaCode);
        childSysDirectoryList.add(sysDirectory);

        params.put("sysDirectoryList", childSysDirectoryList);
        params.put("enterpriseType", enterpriseType);
        return this.baseMapper.queryListByAreaCodeOne(params);
    }

    @Override
    public List<Enterprise> queryListByIndustryCode(String industryCode, int enterpriseType) {
        Map<String, Object> params = new HashMap<>();
        List<SysDirectory> childSysDirectoryList = new ArrayList<SysDirectory>();
        SysDirectory sysDirectory = sysDirectoryService.queryByCode(industryCode);
        childSysDirectoryList.add(sysDirectory);

        params.put("sysDirectoryList", childSysDirectoryList);
        params.put("enterpriseType", enterpriseType);
        return this.baseMapper.queryListByIndustryCode(params);
    }

    @Override
    public List<Enterprise> queryUnitList(String bindingCode) {
        LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Enterprise::getBindingCode, bindingCode);
        wrapper.eq(Enterprise::getEnterpriseType, 0);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<Enterprise> queryUnitSelect(String centerUnitId) {
        LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Enterprise::getId, centerUnitId);
        Enterprise enterprise = this.baseMapper.selectOne(wrapper);
        if (enterprise != null) {
            String bindingStr = (String) redisService.get(enterprise.getAreaCode());
            return enterpriseMapper.querySocialByBindCodes(bindingStr);
        }
        return null;
    }

    @Override
    public PageVo<Enterprise> queryUnitSelectPage(EnterpriseDto enterpriseDto) {
        LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Enterprise::getId, enterpriseDto.getEnterpriseCode());
        Enterprise enterprise = this.baseMapper.selectOne(wrapper);
        if (enterprise != null) {
            String bindingStr = (String) redisService.get(enterprise.getAreaCode());
            HashMap<String, Object> params = new HashMap<>(16);
            params.put("bindingCode",bindingStr);
            params.put("enterpriseName",enterpriseDto.getEnterpriseName());
            params.put("address",enterpriseDto.getAddress());
            params.put("mainPerson",enterpriseDto.getMainPerson());
            params.put("mainPersonMobile",enterpriseDto.getMainPersonMobile());
            Page page = new Page();
            page.setCurrent(enterpriseDto.getPage());
            page.setSize(enterpriseDto.getLimit());
            return new PageVo<Enterprise>(this.baseMapper.querySocialByBindCodesPage(page,params));
        }
        return null;

    }

    @Override
    public List<String> queryBindCodeByUnitAreaCode(String areaCode) {
        return enterpriseMapper.queryBindCodeByUnitAreaCode(areaCode);
    }

    @Override
    public List<Enterprise> queryUnitListByMaintenance(String enterpriseId) {
        return enterpriseMapper.queryUnitListByMaintenance(enterpriseId);
    }

    @Override
    public List<Enterprise> querySocialUnits(HashMap<String, Object> params) {
        return this.baseMapper.querySocialUnits(params);
    }

    @Override
    public List<Enterprise> queryMaintenanceListByUnit(Map<String, Object> params) {
        return enterpriseMapper.queryMaintenanceListByUnit(params);
    }

    @Override
    public Enterprise getInfoByBindingCode(String bindingCode) {
        return enterpriseMapper.queryInfoByBindingCode(bindingCode);
    }

    @Override
    public List<Enterprise> getAllSocialUnit() {
        return enterpriseMapper.queryAllSocialUnits();
    }

    @Override
    public Enterprise queryById(String id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public List<Enterprise> getAllCenter(ArrayList<String> areaList) {
        QueryWrapper<Enterprise> wrapper = new QueryWrapper<>();
        wrapper.eq("enterprise_type", 2)
                .in("area_code", areaList);
        return  this.baseMapper.selectList(wrapper);
    }

    @Override
    public Integer getSocialNum(long unitid) {
        return this.baseMapper.getSocialNum(unitid);
    }

    @Override
    public Integer getSocialOnlineNum(long unitId) {
        return this.baseMapper.getSocialOnlineNum(unitId);
    }

    @Override
    public List<Enterprise> getEnterpriseListByUnitId(String unitId) {
        List<Enterprise> enterpriseList = this.queryUnitSelect(unitId);
        if (CollectionUtils.isEmpty(enterpriseList)) {
            return Collections.emptyList();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("enterpriseList", enterpriseList);
        return this.queryMaintenanceListByUnit(params);
    }

    @Override
    public List<Enterprise> queryByIds(List<String> unitIdList) {
        return this.baseMapper.queryByIds(unitIdList);
    }

    @Override
    public List<Enterprise> selectByType(int i) {
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_type",0);
        return this.baseMapper.selectList(queryWrapper);
    }


}
